home *** CD-ROM | disk | FTP | other *** search
/ Point Programming 1 / PPROG1.ISO / pascal / swag / menu.swg / 0003_Standardize Menu system.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1995-02-28  |  9.1 KB  |  299 lines

  1.  
  2. Unit Menus;
  3. {This unit provides a standardized menu system.}
  4.  
  5. Interface
  6. Uses Crt;
  7.  
  8. Type
  9.     MenuLineType = String[25];
  10.     MenuArryType = Array[1..25] of MenuLineType;
  11.     MenuRec      = Record
  12.         MenuArray  : MenuArryType;
  13.         NbrEntries : Integer;
  14.     End; {MenuRec}
  15.  
  16.     LocationRec = Record
  17.         Col : Integer;
  18.         Row : Integer;
  19.     End;
  20.  
  21.     ScreenLocation = (NW, N, NE, W, C, E, SW, S, SE);
  22.  
  23. Procedure CreateMenu (Var Menu : MenuRec);
  24. {Initializes the menu to empty before starting a new menu}
  25.  
  26. Procedure AddToMenu (Var Menu : MenuRec;
  27.                          NewEntry : MenuLineType);
  28. {Adds a new line entry to the menu being built}
  29.  
  30. Procedure ShowMenu (Var Menu     : MenuRec;
  31.                         Position : ScreenLocation;
  32.                     Var ItemSelected : Integer);
  33. {Displays the current menu on the screen at the location defined by
  34. Position}
  35.  
  36. Procedure ChangeMenuColors (Border, Background, Foreground, SelectBack,
  37.                             SelectFore : Integer);
  38. {Sets the colors used in the menu}
  39.  
  40. Implementation
  41. Type
  42.     MenuColorRec = Record
  43.         MenuBorder : Integer;
  44.         MenuBackGnd : Integer;
  45.         MenuForeGnd : Integer;
  46.         MenuSelectBack : Integer;
  47.         MenuSelectFore : Integer;
  48.     End;
  49.  
  50. Var
  51.     MenuColor : MenuColorRec;
  52.  
  53. Function RowWidth (Var Menu : MenuRec): Integer;
  54. Var
  55.     ix, wide : integer;
  56. Begin
  57.     wide := 0;
  58.     For ix := 1 to Menu.NbrEntries Do
  59.         If Length(Menu.MenuArray[ix]) > wide Then
  60.             wide := Length(Menu.MenuArray[ix]);
  61.     RowWidth := wide;
  62. End;
  63.  
  64. Procedure FindPosition (Var Menu     : MenuRec;
  65.                             Position : ScreenLocation;
  66.                         Var LocOut   : LocationRec);
  67. {This function calculates the beginning position for the first line as
  68.  a column and a row.}
  69.  
  70. Var
  71.     ix : Integer;
  72. Begin
  73.     Case Position of
  74.       NW : Begin
  75.              LocOut.Col := 2;
  76.              LocOut.Row := 2;
  77.            End;
  78.       N  : Begin
  79.              LocOut.Col := 39 - RowWidth(Menu) Div 2;
  80.              LocOut.Row := 2;
  81.            End;
  82.       NE : Begin
  83.              LocOut.Col := 79 - RowWidth(Menu);
  84.              LocOut.Row := 2;
  85.            End;
  86.       W  : Begin
  87.              LocOut.Col := 2;
  88.              LocOut.Row := 12 - Menu.NbrEntries Div 2;
  89.            End;
  90.       C  : Begin
  91.              LocOut.Col := 39 - RowWidth(Menu) Div 2;
  92.              LocOut.Row := 12 - Menu.NbrEntries Div 2;
  93.            End;
  94.       E  : Begin
  95.              LocOut.Col := 79 - RowWidth(Menu);
  96.              LocOut.Row := 11 - Menu.NbrEntries Div 2;
  97.            End;
  98.       SW : Begin
  99.              LocOut.Col := 2;
  100.              LocOut.Row := 25 - Menu.NbrEntries;
  101.            End;
  102.       S  : Begin
  103.              LocOut.Col := 39 - RowWidth(Menu) Div 2;
  104.              LocOut.Row := 25 - Menu.NbrEntries;
  105.            End;
  106.       SE : Begin
  107.              LocOut.Col := 79 - RowWidth(Menu);
  108.              LocOut.Row := 25 - Menu.NbrEntries;
  109.            End;
  110.     End; {Case}
  111. End;
  112.  
  113. Procedure CreateMenu (Var Menu : MenuRec);
  114. {Initializes the menu to empty before starting a new menu}
  115. Begin
  116.     Menu.NbrEntries := 0;
  117.     MenuColor.MenuBorder := Yellow;
  118.     MenuColor.MenuBackGnd := Lightgray;
  119.     MenuColor.MenuForeGnd := Black;
  120.     MenuColor.MenuSelectBack := Cyan;
  121.     MenuColor.MenuSelectFore := Blue;
  122. End;
  123.  
  124. Procedure AddToMenu (Var Menu : MenuRec;
  125.                          NewEntry : MenuLineType);
  126. {Adds a new line entry to the menu being built}
  127. Begin
  128.     inc(Menu.NbrEntries);
  129.     Menu.MenuArray[Menu.NbrEntries] := NewEntry;
  130. End;
  131.  
  132. Procedure ShowMenu (Var Menu : MenuRec;
  133.                         Position : ScreenLocation;
  134.                     Var ItemSelected : Integer);
  135. Var
  136.     CurrPosition,
  137.     HoldPosition : LocationRec;
  138.     ix, wide     : Integer;
  139.     Ch           : Char;
  140. {Displays the current menu on the screen at the location defined by
  141. Position}
  142.  
  143. Begin
  144.          ClrScr;
  145.          FindPosition(Menu, Position, CurrPosition);
  146.          HoldPosition := CurrPosition;
  147.          TextBackGround(MenuColor.MenuBackGnd);
  148.          TextColor(MenuColor.MenuForeGnd);
  149.          ItemSelected := 1;
  150.          GoToXY(CurrPosition.Col, CurrPosition.Row - 1);
  151.          TextBackGround (MenuColor.MenuBorder);
  152.          wide := RowWidth(Menu);
  153.          for ix := 1 to wide + 1 Do
  154.              Write(' ');
  155.          While ItemSelected <= Menu.NbrEntries Do
  156.          Begin
  157.              GoToXY(CurrPosition.Col - 1, CurrPosition.Row);
  158.              TextBackGround (MenuColor.MenuBorder);
  159.              Write (' ');
  160.              TextBackGround (MenuColor.MenuBackGnd);
  161.              Write (Menu.MenuArray[ItemSelected]);
  162.              For ix := Length(Menu.MenuArray[ItemSelected]) to wide Do
  163.                  Write(' ');
  164.              TextBackGround (MenuColor.MenuBorder);
  165.              Write (' ');
  166.              Inc(CurrPosition.Row);
  167.              Inc(ItemSelected);
  168.          End;
  169.          GoToXY(CurrPosition.Col, CurrPosition.Row);
  170.          TextBackGround (MenuColor.MenuBorder);
  171.          for ix := 1 to wide + 1 Do
  172.              Write(' ');
  173.          ItemSelected := 1;
  174.          CurrPosition.Row := HoldPosition.Row;
  175.          Repeat
  176.              GoToXY(CurrPosition.Col, CurrPosition.Row);
  177.              TextBackground (MenuColor.MenuSelectBack);
  178.              TextColor (MenuColor.MenuSelectFore);
  179.              Write (Menu.MenuArray[ItemSelected]);
  180.              For ix := Length(Menu.MenuArray[ItemSelected]) to wide Do
  181.                  Write (' ');
  182.              Ch := Readkey;
  183.              TextBackGround (MenuColor.MenuBackGnd);
  184.              TextColor (MenuColor.MenuForeGnd);
  185.              if Ch = #0 Then
  186.              Begin
  187.                  Ch := ReadKey;
  188.                  GoToXY(CurrPosition.Col, CurrPosition.Row);
  189.                  Write (Menu.MenuArray[ItemSelected]);
  190.                  For ix := Length(Menu.MenuArray[ItemSelected]) to wide Do
  191.                      Write (' ');
  192.                  Case Ch of
  193.                  #80 : Begin
  194.                        Inc(CurrPosition.Row);
  195.                        Inc(ItemSelected);
  196.                        If ItemSelected > Menu.NbrEntries Then
  197.                        Begin
  198.                           ItemSelected := 1;
  199.                           CurrPosition.Row := HoldPosition.Row;
  200.                          End;
  201.                        End;
  202.                  #72  : Begin
  203.                        Dec(CurrPosition.Row);
  204.                        Dec(ItemSelected);
  205.                        If ItemSelected < 1 Then
  206.                        Begin
  207.                           CurrPosition.Row := Menu.NbrEntries + HoldPosition.Row
  208.                            - 1;
  209.                           ItemSelected := Menu.NbrEntries;
  210.                          End;
  211.                        End;
  212.                   End;
  213.              End;
  214.          Until (Ch = #27) or (Ch = #13);
  215. End;
  216.  
  217. Procedure ChangeMenuColors (Border, Background, Foreground, SelectBack,
  218.                             SelectFore : Integer);
  219. {Sets the colors used in the menu}
  220. Begin
  221.     MenuColor.MenuBorder := Border;
  222.     MenuColor.MenuBackGnd := Background;
  223.     MenuColor.MenuForeGnd := Foreground;
  224.     MenuColor.MenuSelectBack := SelectBack;
  225.     MenuColor.MenuSelectFore := SelectFore;
  226. End;
  227.  
  228. End.
  229.  
  230. { ----------------------DEMO PROGRAM FOLLOWS ----------------------}
  231.  
  232.  
  233. Program MenuDemo;
  234. Uses Menus, Dos, Crt;
  235. Var
  236.     Menu : MenuRec;
  237.     ItemSelected : Integer;
  238. Begin
  239.     CreateMenu(Menu);
  240.     AddToMenu(Menu, 'DISPLAY MEMBERS');
  241.     AddToMenu(Menu, 'ADD A MEMBER');
  242.     AddToMenu(Menu, 'DELETE A MEMBER');
  243.     AddToMenu(Menu, 'MEMBER FINANCES');
  244.     AddToMenu(Menu, 'QUIT');
  245.     ShowMenu(Menu, NW, ItemSelected);
  246.     TextBackGround (Black);
  247.     TextColor (White);
  248.     ClrScr;
  249.     Writeln ('SELECTED ', Menu.MenuArray[ItemSelected]);
  250.     Readln;
  251.     ShowMenu(Menu, N, ItemSelected);
  252.     TextBackGround (Black);
  253.     TextColor (White);
  254.     ClrScr;
  255.     Writeln ('SELECTED ', Menu.MenuArray[ItemSelected]);
  256.     Readln;
  257.     ShowMenu(Menu, NE, ItemSelected);
  258.     TextBackGround (Black);
  259.     TextColor (White);
  260.     ClrScr;
  261.     Writeln ('SELECTED ', Menu.MenuArray[ItemSelected]);
  262.     Readln;
  263.     ShowMenu(Menu, W, ItemSelected);
  264.     TextBackGround (Black);
  265.     TextColor (White);
  266.     ClrScr;
  267.     Writeln ('SELECTED ', Menu.MenuArray[ItemSelected]);
  268.     Readln;
  269.     ShowMenu(Menu, C, ItemSelected);
  270.     TextBackGround (Black);
  271.     TextColor (White);
  272.     ClrScr;
  273.     Writeln ('SELECTED ', Menu.MenuArray[ItemSelected]);
  274.     Readln;
  275.     ShowMenu(Menu, E, ItemSelected);
  276.     TextBackGround (Black);
  277.     TextColor (White);
  278.     ClrScr;
  279.     Writeln ('SELECTED ', Menu.MenuArray[ItemSelected]);
  280.     Readln;
  281.     ShowMenu(Menu, SW, ItemSelected);
  282.     TextBackGround (Black);
  283.     TextColor (White);
  284.     ClrScr;
  285.     Writeln ('SELECTED ', Menu.MenuArray[ItemSelected]);
  286.     Readln;
  287.     ShowMenu(Menu, S, ItemSelected);
  288.     TextBackGround (Black);
  289.     TextColor (White);
  290.     ClrScr;
  291.     Writeln ('SELECTED ', Menu.MenuArray[ItemSelected]);
  292.     Readln;
  293.     ShowMenu(Menu, SE, ItemSelected);
  294.     TextBackGround (Black);
  295.     TextColor (White);
  296.     ClrScr;
  297.     Writeln ('SELECTED ', Menu.MenuArray[ItemSelected]);
  298.     Readln;
  299. End.